package org.x.casemodule.controller;

import com.mybatisflex.core.paginate.Page;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.x.casemodule.entity.DocumentDraftEntity;
import org.x.casemodule.entity.DocumentTemplateEntity;
import org.x.casemodule.entity.GeneratedDocumentEntity;
import org.x.casemodule.entity.document;
import org.x.casemodule.service.DocumentService;
import lombok.Data;
import org.x.common.config.Result;
import org.x.common.enums.LogAsync;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/doc")
public class DocumentController {

    @Autowired
    private DocumentService documentService;

    @GetMapping("/hello")
    public Result hello() {
        return Result.ok("Hello, World!");
    }
    //分页查询文书生成表
    @LogAsync("查询所有生成文书表1")
    @GetMapping("/list")
    public Result list() {
        List<GeneratedDocumentEntity> list = documentService.list();
        return Result.ok(list);
    }
    //根据id查询详情


    // 1. 申请书生成接口
    @LogAsync("申请书生成接口")
    @PostMapping("/application")
    public ResponseEntity<ApiResponse<GeneratedDocumentEntity>> generateApplication(@RequestBody ApplicationRequest request) {
        GeneratedDocumentEntity document = documentService.generateApplication(request.getCaseId());
        return ResponseEntity.ok(ApiResponse.success(document));
    }

    // 2. 法律条款引用接口
    @LogAsync("法律条款引用接口")
    @PostMapping("/laws")
    public ResponseEntity<ApiResponse<List<document.LegalProvision>>> getRelatedLaws(
            @RequestBody LawReferenceRequest request) {
        List<document.LegalProvision> laws = documentService.getRelatedLaws(
                request.getDocId(), request.getPosition());
        return ResponseEntity.ok(ApiResponse.success(laws));
    }

    // 3. 文书模板管理接口
    @LogAsync("文书模板管理接口")
    @GetMapping("/templates")
    public ResponseEntity<ApiResponse<List<DocumentTemplateEntity>>> getTemplates() {
        List<DocumentTemplateEntity> templates = documentService.getAllTemplates();
        return ResponseEntity.ok(ApiResponse.success(templates));
    }

    // 3. 添加新的文书模板
    @LogAsync("添加新的文书模板")
    @PostMapping("/templates")
    public ResponseEntity addTemplate(
            @RequestBody DocumentTemplateEntity template) {
        DocumentTemplateEntity savedTemplate = documentService.addTemplate(template);
        return ResponseEntity.ok(savedTemplate);
    }

    // 4. 文书草稿保存接口
    @LogAsync("文书草稿保存接口")
    @PostMapping("/draft")
    public ResponseEntity<ApiResponse<DocumentDraftEntity>> saveDraft(
            @RequestBody DraftSaveRequest request) {
        DocumentDraftEntity draft = documentService.saveDraft(
                request.getDocId(), request.getContent());
        return ResponseEntity.ok(ApiResponse.success(draft));
    }

    // 5. 文书导出接口
//    @LogAsync("文书导出接口")
//    @PostMapping("/export")
//    public ResponseEntity<ApiResponse<String>> exportDocument(
//            @RequestBody ExportRequest request) {
//        String downloadUrl = documentService.exportDocument(
//                request.getDocId(), request.getFormat());
//        return ResponseEntity.ok(ApiResponse.success(downloadUrl));
//    }
    // 5. 文书导出接口
    @LogAsync("文书导出接口")
    @PostMapping("/export")
    public void exportDocument(
            @RequestBody ExportRequest request,
            HttpServletResponse response) throws IOException {
        documentService.exportDocument(request.getDocId(), request.getFormat(), response);
    }

    // 6. 文书签名接口
    @LogAsync("文书签名接口")
    @PostMapping("/sign")
    public ResponseEntity<ApiResponse<String>> signDocument(
            @RequestBody SignRequest request) {
        String signedDocUrl = documentService.signDocument(
                request.getDocId(), request.getSignImage());
        return ResponseEntity.ok(ApiResponse.success(signedDocUrl));
    }

    // 请求和响应对象
    @Data
    static class ApplicationRequest {
        private String caseId;
    }

    @Data
    static class LawReferenceRequest {
        private Long docId;
        private int position;
    }

    @Data
    static class DraftSaveRequest {
        private Long docId;
        private String content;
    }

    @Data
    static class ExportRequest {
        private Long docId;
        private String format; // pdf或word
    }

    @Data
    static class SignRequest {
        private Long docId;
        private String signImage; // base64编码的签名图片
    }

    @Data
    static class ApiResponse<T> {
        private int code;
        private T data;

        public static <T> ApiResponse<T> success(T data) {
            ApiResponse<T> response = new ApiResponse<>();
            response.setCode(200);
            response.setData(data);
            return response;
        }
    }
}
